home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / archival / ftp / BFTP.312 / bftp_share.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-29  |  7.4 KB  |  318 lines

  1. /************************************************************************
  2.  *                                    *
  3.  *     Background File Transfer Program (BFTP)                *
  4.  *                                    *
  5.  *    Written at USC/Information Sciences Institute            *
  6.  *    September, 1988                            *
  7.  *                                    *
  8.  *      BFTP is Public Domain, and may be used for any purpose as    *
  9.  *      long as this notice is not removed.  USC-ISI does not assume    *
  10.  *    any responsibility for the correctness, performance, or use    *
  11.  *    of this software.                        *
  12.  *                                    *
  13.  ************************************************************************/
  14. /*
  15.  *    bftp_share.c
  16.  *
  17.  *    These global variables and routines are are common to the "bftp", 
  18.  *    tty-style user interface, and the "bftptool", SunView user interface.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <ctype.h>
  23. #include <strings.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <sys/param.h>
  27. #include "bftp.h"
  28.  
  29. extern char
  30.     **environ,
  31.     *getenv(),
  32.         *getlogin(),
  33.     *malloc();
  34.  
  35. char bftp_dir[MAXPATHLEN],
  36. #ifdef BSD4_3
  37.      def_user[10],
  38. #else
  39.      def_user[L_cuserid],
  40. #endif
  41.      def_hostname[80],
  42.      def_mailbox[101],
  43.      *ptrs[MAX_SAVES+1];
  44.  
  45. FILE *listp = NULL;     
  46. char filename[100];
  47.      
  48. u_long start = 0;     
  49.      
  50. struct hostinfo src, 
  51.         dst,
  52.         nullhost = {"","","","","","",DEFAULT_PORT};
  53. struct fileinfo fil,
  54.         defaultfile = {"A N",'F','S',COPY,STOR,FALSE};
  55. struct reqinfo  nullreq = {"","","","",0,0};
  56.  
  57. char *version = "Version 3.12  6/29/90";    
  58.     
  59. char *introduction = "\n\
  60. Introduction:\n\
  61. \n\
  62.   This Background File Transfer program may be used to submit a request\n\
  63.   to have a file transfered at some time in the future.\n\
  64. \n";
  65.   
  66. char *background = "\n\
  67. Additional background information on how BFTP works:\n\
  68. \n\
  69.   BFTP makes use of third party FTP, so the source and the destination\n\
  70.   hosts do not have to be operational at the time that the request is\n\
  71.   submitted.  Transfers are scheduled locally via the system 'at' command.\n\
  72.   Therefore the exact time that a file tranfer will take place depends on\n\
  73.   how often jobs are run from the system 'at' queue; for more details, see\n\
  74.   the system manual pages for 'at', 'cron', and 'crontab'.\n\
  75. \n\
  76.   For a transfer to succeed, either the source or the destination host must\n\
  77.   support the FTP passive command.  (4.2BSD doesn't, 4.3BSD does!)\n\
  78. \n\
  79.   To set the directory where BFTP stores request files to something other\n\
  80.   than ones home directory, exit this program and use the system 'setenv'\n\
  81.   command, for example 'setenv BFTPDIR ~deschon/.bftp'.\n\
  82. \n\
  83. Please report bugs to Annette DeSchon <deschon@isi.edu>, 213-822-1511.\n\
  84. \n";
  85.  
  86. void
  87. close_listp()
  88. {
  89.    if (listp) {
  90.       pclose(listp);
  91.       listp = NULL;
  92.       }
  93. }
  94.     
  95. char *    
  96. get_request_file(password)
  97.    char *password;
  98. {
  99.    char temp[MAXPATHLEN*2];
  100.     
  101.    if (!listp) {
  102.       sprintf(temp,"grep -l '^%s' *.req /dev/null 2> /dev/null\n", password);
  103.       listp = popen(temp,"r");
  104.       getwd(temp);
  105.       }
  106.    if (!listp)
  107.       return(NULL);
  108.    else 
  109.       if (EOF != fscanf(listp,"%s",filename)) {
  110.          return(filename);
  111.      }
  112.       else {
  113.      close_listp();
  114.      return(NULL);
  115.          }
  116. }    
  117.  
  118. void
  119. init_req(req)
  120.    struct reqinfo *req;
  121. {
  122.    *req->rpasswd = '\0';
  123.    strcpy(req->mailbox, def_mailbox);
  124.    *req->mailfile = '\0';
  125.    *req->cmdfile = '\0';
  126.    req->interval = 15;
  127.    req->ntries = 5;
  128. }
  129.     
  130. void 
  131. init_user()
  132. {
  133.    char *cp;
  134.     
  135. #ifdef BSD4_3
  136.    if ((cp = getlogin()) != NULL)
  137.       strcpy(def_user, cp);
  138.    else
  139.       def_user[0] = '\0';
  140. #else
  141.    (void)cuserid(def_user);
  142. #endif
  143.    if (strlen(def_user))
  144.       sprintf(def_mailbox, "%s@%s", def_user, SITESTR);
  145.    else
  146.       def_mailbox[0] = '\0';
  147.  
  148.    if ((cp = getenv("BFTPDIR")) == NULL) {
  149.       if ((cp = getenv("HOME")) == NULL)
  150.          cp = ".";
  151.       /* make sure that the directory exists */     
  152.       if (chdir(cp) < 0) {
  153.      fprintf(stderr,"bftp: Directory does not exist: %s\n",cp);
  154.      fprintf(stderr,"Check environment variables BFTPDIR and HOME.\n");
  155.      exit(1);
  156.          }
  157.       strcpy(bftp_dir, cp);
  158.       if (bftp_dir[strlen(bftp_dir)-1] != '/')
  159.          strcat(bftp_dir, "/");
  160.       }
  161.    else {
  162.       /* make sure that the directory exists */     
  163.       if (chdir(cp) < 0) {
  164.      strcpy(bftp_dir, getenv("HOME"));
  165.      if (bftp_dir[strlen(bftp_dir)-1] != '/')
  166.         strcat(bftp_dir, "/");
  167.      strcat(bftp_dir, cp);
  168.      if (bftp_dir[strlen(bftp_dir)-1] != '/')
  169.         strcat(bftp_dir, "/");
  170.      if (chdir(bftp_dir) < 0) {
  171.         fprintf(stderr,"bftp: Directory does not exist: %s\n",cp);
  172.         fprintf(stderr,"Check environment variables BFTPDIR and HOME.\n");
  173.         exit(1);
  174.             }
  175.          }
  176.       else {
  177.          strcpy(bftp_dir, cp);
  178.      if (bftp_dir[strlen(bftp_dir)-1] != '/')
  179.         strcat(bftp_dir, "/");
  180.          }
  181.       }
  182.    
  183.    gethostname(def_hostname, sizeof(def_hostname));
  184. } /* init_user */
  185.  
  186. char **
  187. get_save_files()
  188. {
  189.    FILE *listp;
  190.    int tmp, i = 0, len;
  191.    char prefix[80], temp[80], filename[80];
  192.    
  193.    sprintf(prefix,"%s.", SAVEPREFIX);
  194.    len = strlen(prefix);
  195.    sprintf(temp,"ls -1 %s* 2>/dev/null\n", prefix);
  196.    if (listp = popen(temp,"r")) {
  197.       for (tmp = fscanf(listp,"%s",filename);
  198.              tmp != EOF && i<MAX_SAVES;
  199.              tmp = fscanf(listp,"%s",filename), i++) {
  200.      if (!strncmp(prefix,filename, len)) {
  201.         ptrs[i] = malloc(strlen(filename+len));    
  202.         strcpy(ptrs[i], filename+len);
  203.         }
  204.      else
  205.         break;
  206.      }
  207.       pclose(listp);
  208.       }
  209.    ptrs[i] = NULL;
  210.    return(ptrs);
  211. }
  212.  
  213. void
  214. cancel_msg(reqfile, req, filename, listfile)
  215.    char *reqfile;
  216.    struct reqinfo *req;
  217.    char *filename, *listfile;
  218. {
  219.    FILE *msgfp;
  220.    int jobno;
  221.    char temp[100], subject[100];
  222.  
  223.    format_time(0, temp);
  224.    if ((strlen(req->mailfile)!=0) && 
  225.        ((msgfp = fopen(req->mailfile,"a")) != NULL)) {
  226.     fprintf(msgfp,"\n  %s: request cancelled.\n",temp);
  227.     fclose(msgfp);
  228.     }
  229.    /* dequeue it from atq */
  230.    if (jobno = find_job(reqfile)) {
  231.       sprintf(temp, "atrm %d 1> /dev/null 2>&1",jobno);
  232.       system(temp);
  233.       }
  234.       
  235.    sprintf(temp, "%s -- Cancelled", filename);
  236.    if (!strlen(listfile)) {
  237.       get_id(listfile,reqfile);
  238.       strcat(listfile,".list");
  239.       }
  240.    finish_req(reqfile, req, temp, listfile);
  241.       
  242. } /* cancel_msg */
  243.  
  244. boolean
  245. request_queued(filename)
  246.    char *filename;
  247. {
  248.    FILE *atqfp;
  249.    char temp[100], id[20];
  250.    boolean found = FALSE;
  251.    
  252.    get_id(id, filename);
  253.    
  254.    /* grep atq results to see whether this request is there */
  255.    sprintf(temp,"atq | grep %s", id);
  256.    if (atqfp = popen(temp,"r")) {
  257.       found = (fgets(temp, sizeof(temp), atqfp))? TRUE:FALSE;
  258.       pclose(atqfp);
  259.       }
  260.  
  261.    return(found);
  262. }
  263.  
  264. boolean       
  265. empty_str(xxx)
  266.     char *xxx;
  267. {
  268.     char *pch;
  269.     
  270.     if (!strlen(xxx))
  271.         return(TRUE);
  272.     for (pch = xxx;*pch && isspace(*pch);pch++) ;
  273.     return((!*pch)?TRUE:FALSE);
  274. }
  275.  
  276. boolean
  277. mailbox_ok(name,errorstr)
  278.    char *name;
  279.    char *errorstr;
  280. {
  281.    char *cp;
  282.     
  283.    if ((cp = index(name, '@')) == NULL) {
  284.       if (errorstr)
  285.      strcpy(errorstr, "Mailbox format is 'person@host'.\n");
  286.       return(FALSE);
  287.       }
  288.    if (! gethostbyname(++cp)) {
  289.       if (errorstr)
  290.      sprintf(errorstr, "Illegal host name in mailbox: '%s'\n", cp);
  291.       return(FALSE);
  292.       }
  293.  
  294.    if (errorstr) errorstr[0] = '\0';
  295.    return(TRUE);
  296. }
  297.  
  298. boolean
  299. parse_date(dp, tdate)
  300.    char *dp;
  301.    time_t *tdate;        /* parsed date and time */
  302. {
  303.     struct tm datetm;
  304.  
  305.     /*
  306.       parsedate (str, tmp, settm, select, err, gmt)
  307.                char *str;
  308.          struct tm *tmp;
  309.          int settm, select, err;
  310.          long *gmt;
  311.     */     
  312.  
  313.     if (parsedate(dp, &datetm, TRUE, FALSE, TRUE, tdate) < 0)
  314.        return(FALSE);
  315.  
  316.     return(TRUE);
  317. } /* parse_date */
  318.